home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1258 / samples / switchtr.jav < prev    next >
Text File  |  1996-03-09  |  6KB  |  284 lines

  1. // SwitchTrack.java
  2.  
  3. /*
  4.  * Copyright (c) 1996 by Corey Trager.
  5.  *
  6.  * Permission to use, copy, and distribute this software for
  7.  * any purpose and without fee is hereby granted provided
  8.  * that this copyright notice appears in all copies.
  9.  *
  10.  */
  11.  
  12. // graphically, a hybrid of StraightTrack and CurvedTrack.
  13. // More logic, however, to support the automatic toggling
  14. // of the switch.
  15.  
  16.  
  17. import java.awt.Graphics;
  18. import java.awt.Color;
  19.  
  20. class SwitchTrack extends Track {
  21.  
  22.     // Main lines
  23.     Arc arc;
  24.     Line ln;
  25.     static final int LEFT = 0;
  26.     static final int RIGHT = 1;
  27.     int side;
  28.     boolean toggle = true;
  29.     
  30.     public SwitchTrack (int x, int y, int side)
  31.     {
  32.         tieCnt = 5;
  33.         connectionCnt = 3;
  34.         this.side = side;
  35.         
  36.         instantiateObjects();
  37.                 
  38.         ox = (double) x;
  39.         oy = (double) y;
  40.  
  41.         restore ();        
  42.     }
  43.  
  44.  
  45.     void setMainLines () {
  46.         if (side == RIGHT)
  47.             arc = new Arc(
  48.                 new DblPoint(x,y+40),
  49.                 40,
  50.                 Geometry.FORTY_FIVE_DEGREES,
  51.                 Geometry.NINETY_DEGREES);
  52.         else
  53.             arc = new Arc(
  54.                 new DblPoint(x+40,y+40),
  55.                 40,
  56.                 Geometry.NINETY_DEGREES,
  57.                 Geometry.degreesToRadians (135.0));
  58.             
  59.         ln = new Line(new DblPoint(x,y),new DblPoint(x+40,y));
  60.     }
  61.     
  62.     void setTies () {
  63.  
  64.         double up = ln.c.y + TIE_SIZE;
  65.         double down = ln.c.y - TIE_SIZE;
  66.         double inc = 10.0;
  67.         
  68.         if (side == RIGHT) {
  69.             for (int i = 0; i < 3; i++) {
  70.                 ties[i].v1.x = ties[i].v2.x
  71.                     = ln.v1.x + inc + (i * inc);
  72.                 ties[i].v1.y = up;
  73.                 ties[i].v2.y = down;
  74.             }
  75.         }
  76.         else {
  77.             for (int i = 0; i < 3; i++) {
  78.                 ties[i].v1.x = ties[i].v2.x
  79.                     = ln.v2.x - inc - (i * inc);
  80.                 ties[i].v1.y = up;
  81.                 ties[i].v2.y = down;
  82.             }
  83.         }
  84.  
  85.  
  86.         double centerX = arc.c.x;
  87.         double centerY = arc.c.y;
  88.         
  89.         double theta;
  90.         DblPoint vector = DblPoint.vector (Geometry.ORIGIN, arc.c);
  91.         DblPoint pt = new DblPoint (0,0);
  92.         
  93.         for (int i = 3; i < tieCnt; i++) {
  94.             ties[i].v1.x = centerX;
  95.             ties[i].v1.y = centerY + 3;
  96.             ties[i].v2.x = centerX;
  97.             ties[i].v2.y = centerY - 3;
  98.             if (side == RIGHT) {
  99.                 theta = Geometry.degreesToRadians (-15.0 * (i - 2));
  100.                 ties[i].rotate (vector, theta);
  101.             }
  102.             else {
  103.                 theta = Geometry.degreesToRadians (15.0 * (i - 2));
  104.                 ties[i].rotate (vector, theta);
  105.             }
  106.             pt.x = Math.sin (-theta) * arc.radius;
  107.             pt.y = - Math.cos (theta) * arc.radius;
  108.             ties[i].translate (pt);
  109.         }
  110.     }
  111.  
  112.     void setConnectionPoints () {
  113.         if (side == RIGHT) {
  114.             connectionPoints[0].x = ln.v1.x;
  115.             connectionPoints[0].y = ln.v1.y;
  116.             connectionPoints[1].x = ln.v2.x;
  117.             connectionPoints[1].y = ln.v2.y;
  118.             connectionPoints[2].x = arc.v2.x;
  119.             connectionPoints[2].y = arc.v2.y;
  120.         }
  121.         else {
  122.             connectionPoints[0].x = ln.v2.x;
  123.             connectionPoints[0].y = ln.v2.y;
  124.             connectionPoints[1].x = ln.v1.x;
  125.             connectionPoints[1].y = ln.v1.y;
  126.             connectionPoints[2].x = arc.v1.x;
  127.             connectionPoints[2].y = arc.v1.y;
  128.         }
  129.         
  130.         cx = ln.c.x;
  131.         cy = ln.c.y;
  132.     }
  133.  
  134.     public void restore() {
  135.  
  136.         x = ox;
  137.         y = oy;
  138.         super.restore();
  139.         setMainLines();
  140.         setTies();
  141.         setConnectionPoints();
  142.     }
  143.  
  144.     public void move(int argX, int argY) {
  145.  
  146.         DblPoint vector = ln.moveTo (
  147.             (double) argX, (double) argY);
  148.  
  149.         arc.translate(vector);
  150.         
  151.         for (int i = 0; i < tieCnt; i++)
  152.             ties[i].translate(vector);
  153.  
  154.         setConnectionPoints();
  155.  
  156.     }
  157.  
  158.     public void rotate(double theta) {
  159.     
  160.         DblPoint vector;
  161.  
  162.         if (transformMode == ROTATE_MODE_XY) {
  163.             if (side == RIGHT)
  164.                 vector = DblPoint.vector (Geometry.ORIGIN, ln.v1);
  165.             else
  166.                 vector = DblPoint.vector (Geometry.ORIGIN, ln.v2);
  167.         }
  168.         else if (transformMode == ROTATE_MODE_PQ) {
  169.             if (side == RIGHT)
  170.                 vector = DblPoint.vector (Geometry.ORIGIN, ln.v2);
  171.             else
  172.                 vector = DblPoint.vector (Geometry.ORIGIN, ln.v1);
  173.         }
  174.         else
  175.             vector = DblPoint.vector (Geometry.ORIGIN, ln.c);
  176.  
  177.         ln.rotate(vector, theta);
  178.         arc.rotate(vector, theta);
  179.         for (int i = 0; i < tieCnt; i++)
  180.             ties[i].rotate(vector, theta);
  181.         
  182.         setConnectionPoints();
  183.     }
  184.  
  185.     public void draw (Graphics g) {
  186.  
  187.         if (transformMode == NOT_GRIPPED)
  188.             g.setColor(Color.black);
  189.         else
  190.             g.setColor(Color.blue);
  191.         
  192.         ln.draw (g);
  193.         arc.draw (g);
  194.         
  195.         super.draw(g);
  196.     }
  197.  
  198.     // Get track connected to this track which is
  199.     // NOT the same as the incoming track
  200.     int getNext (int id) {
  201.  
  202.         // check the connection at the
  203.         // single end;
  204.         
  205.         if (connections[0] == id) {
  206.             if (toggle) {
  207.                 if (connections[1] != EMPTY) {
  208.                     toggle = false;
  209.                     return connections[1];
  210.                 }
  211.                 else {
  212.                     return -1;
  213.                 }
  214.             }
  215.             else {    
  216.                 if (connections[2] != EMPTY) {
  217.                     toggle = true;
  218.                     return connections[2];
  219.                 }
  220.                 else {
  221.                     return -1;
  222.                 }    
  223.             }
  224.         }
  225.         else {
  226.             if (connections [0] != EMPTY)
  227.                 return connections[0];
  228.             else
  229.                 return -1;
  230.         }
  231.     }
  232.  
  233.     int appendRunPoints (DblPoint pts[], int index, int id) {
  234.         // coming in from the single side - toggle
  235.         // the way we leave
  236.         if (connections[0] == id) {
  237.             if (toggle) {
  238.                 pts[index] = new DblPoint (ties[0].c);
  239.                 index++;
  240.                 pts[index] = new DblPoint (ties[1].c);
  241.                 index++;
  242.                 pts[index] = new DblPoint (ties[2].c);
  243.                 index++;
  244.                 pts[index] = new DblPoint (connectionPoints[1]);
  245.                 index++;
  246.             }
  247.             else {
  248.                 pts[index] = new DblPoint (ties[3].c);
  249.                 index++;
  250.                 pts[index] = new DblPoint (ties[4].c);
  251.                 index++;
  252.                 pts[index] = new DblPoint (connectionPoints[2]);
  253.                 index++;
  254.             }
  255.         }
  256.         else if (connections[1] == id) {
  257.             // coming in via the straight track
  258.             pts[index] = new DblPoint (ties[2].c);
  259.             index++;
  260.             pts[index] = new DblPoint (ties[1].c);
  261.             index++;
  262.             pts[index] = new DblPoint (ties[0].c);
  263.             index++;
  264.             pts[index] = new DblPoint (connectionPoints[0]);
  265.             index++;
  266.         } else {
  267.             // coming in via the curved track
  268.             pts[index] = new DblPoint (ties[4].c);
  269.             index++;
  270.             pts[index] = new DblPoint (ties[3].c);
  271.             index++;
  272.             pts[index] = new DblPoint (connectionPoints[0]);
  273.             index++;
  274.         }
  275.  
  276.         return index;
  277.     }
  278.  
  279.     void prepareForCircuitInquiry ()
  280.     {
  281.         toggle = true;
  282.     }
  283. }
  284.